In this lesson, you will learn about the “While” Loop control statement in C# and how to use it. Also, a brief explanation of nested While loop will be provided at the end of the lesson.

What is the “while” Loop in C#?

The “while” loop continually executes a block of code repeatedly until an expression is evaluated to false. If the expression is evaluated as true, the loop executes the loop body statement, and then evaluates the expression again. The loop will exit and once the expression is evaluated as false.

Syntax of “while” Loop in C#

            while (boolean expression)
            {
                //execute the loop statement as long as the expression is true    
            }

Flowchart of “while” Loop

c# while loop flowchart

Example of “while” Loop in C#

        static void Main(string[] args)
        {
            int i = 1;
            while (i < 10)
            {
                Console.WriteLine("The value of i is: " + i);
                i = i + 1;
            }
            Console.ReadKey();
        }

Output

c# while loop

In the code above, the while loop evaluates the value of ‘i’. If ‘i’ is less than 10, the loop body statement will execute (print the value of ‘i’). The value of ‘i’ will be increased by 1 at the loop body statement. The “while” loop will iterate again until the expression is evaluated to be false.

It’s important to remember to increase the value of i, otherwise, the loop will become infinite, which will loop forever causing the application to crash.

while loop break statement

The break statement in the while look is used to exit the loop. Exiting a loop is comment practice in programming. Use the ‘break’ statement in your program if you want to exist a ‘while’ loop once a condition is met, such as finding a specific value while looping through a list of values.

Flowchart of ‘break’ in ‘while’ Loop

c# while loop break flowchart

Example of using ‘break’ in ‘while’ loop

        static void Main(string[] args)
        {
            int i = 0;
            while (true)
            {
                Console.WriteLine("Value of i is: " + i);
                i = i + 1;
                if (i > 10)
                    break;
            }
            Console.ReadKey();
        }

Output

c# while loop example

while loop continue statement

The continue statement in C# works similar to the break statement. However, it doesn’t exit the loop but instead, it continues to the next iteration skipping the code inside the loop body.

Flowchart of ‘continue’ in ‘while’ Loop

c# while loop continue flowchart

Example of using ‘continue’ in ‘while’ loop

        static void Main(string[] args)
        {
            int i = 1;
            /* do loop execution */
            while (i < 10)
            {
                if (i == 5)
                {
                    i = i + 1;
                    // skip the iteration
                    continue;
                }
                Console.WriteLine("value of i is: " + i);
                i = i + 1;
            }
            Console.ReadKey();
        }

Output

c# while loop example

Nested while Loops

Nested loops are allowed in C# and are very useful when looping through a 2 dimension metric, an excel spreadsheet, or a DataGrid.

Example of nested ‘while’ Loops in C#

        static void Main(string[] args)
        {
            int i = 0;
            while (i < 3)
            {
                Console.WriteLine("Value of i: {0}", i);
                int j = 1; i++; while (j < 3)
                {
                    Console.WriteLine("Value of j: {0}", j);
                    j++;
                }
                Console.WriteLine("----");
            }
            Console.ReadKey();
        }

Output

c# while loop example

Last modified: July 21, 2018

Comments

Write a Reply or Comment

Your email address will not be published.